var data;
var checkObject;
var pubInfo;
var cid;
var pcid;
var productId;
var messageNo;
var checkObject;
var eMessageObject;
var workImgUrl;

$(function() {
	data = datax;
	checkObject = checkObjectx;
	pubInfo = pubInfox;
	cid = pubInfo.cid;
	pcid = pubInfo.pcid;
	productId = pubInfo.publishProduct;
	messageNo = pubInfo.messageNo;
	eMessageObject = pubInfo.eMessageObject == null ? checkObject : pubInfo.eMessageObject;
	workImgUrl = workImgUrlx;
})

$(function() {
	var companyUrl = '/CMS/select/getCompanyList.ht';
	initCompanySelect2("pcid", companyUrl, null, "companyName");
	var type = pubInfo.infoType == null ? $('#infoType').val() : pubInfo.infoType;
	showTr(type);
	initPublishProduct(type);
	initCommmunity();
	$('#infoType').change(function() {
		showTr(this.value);
		initPublishProduct(this.value);
		search();
		initCommmunity();
		var p1=$("#publishProduct").children('option:selected').val();
		if(p1=="PAYMENT"){
			editor.txt.html("<p style='text-align: left;'>尊敬的业主:</p>");
		}else{
			editor.txt.html("");
		}
	});
	$('#pcid').change(function() {
		search();
		initCommmunity();
	});

	$("input[name=eMessageObject][value=" + eMessageObject + "]").click();

	if (messageNo) {
		$('#infoType').val(type);
		$('#status').val(pubInfo.status);
		initUlForUpdate();
	}

	$(":radio").click(function() {
		show_Ul_tr($(this).val());
	});

	setValidate($("#frm"), {
		rules : {
			infoType : {
				required : true
			},
			pcid : {
				required : true
			},
			cid : {
				required : true
			},
			title : {
				required : true,
				maxlength : 60
			},
			publishProduct : {
				required : true
			},
			validdate : {
				required : true
			},
			invalidDate : {
				required : true
			},
			status : {
				required : true
			},
			content : {
				required : true,
				maxlength : 1000
			}
		},
		messages : {
			infoType : {
				required : "请选择类型"
			},
			pcid : {
				required : "请选择公司"
			},
			cid : {
				required : "请选择小区"
			},
			title : {
				required : "请输入标题",
				maxlength : "标题太长"
			},
			publishProduct : {
				required : "请选择发布产品"
			},
			validdate : {
				required : "请选择生效时间"
			},
			invalidDate : {
				required : "请选择失效时间"
			},
			status : {
				required : "请选择状态"
			},
			content : {
				required : "请输入内容",
				maxlength : "内容太长"
			}
		}
	});
	
	$("#publishProduct").change(function(){
		editor.txt.html("");
		var p1=$(this).children('option:selected').val();
		if(p1=="PAYMENT"){
			editor.txt.html("<p style='text-align: left;'>尊敬的业主:</p>");
		}else{
			editor.txt.html("");
		}
	});
});

function wordCount(id, limit) {
	var elem = $("#" + id);
	if (elem.next()) {
		elem.next().remove();
	}
	if (elem.val().length > limit) {
		elem.val(elem.val().substring(0, limit));
	}
	elem.parent().append("<span class='red'>您还可以输入" + (limit - elem.val().length) + "个字</span>");
}

function showTr(str) {
	if (str == 0) {
		$('#companyTr').hide();
		$('#communityTr').hide();
		$('#company_objTr').hide();
		$('#community_ul_tr').hide();
	} else if (str == 1) {
		$('#companyTr').show();
		$('#communityTr').show();
		$('#company_objTr').hide();
		$('#community_ul_tr').hide();
	} else {
		$('#companyTr').show();
		$('#communityTr').hide();
		$('#company_objTr').show();
		if ($("input[name=eMessageObject]:checked").val() == 2) {
			$('#community_ul_tr').show();
		}
	}
}

function show_Ul_tr(str) {
	if (str == 0) {
		$('#community_ul_tr').hide();
	} else if (str == 1) {
		$('#community_ul_tr').hide();
	} else {
		$('#community_ul_tr').show();
	}
}

function initPublishProduct(date) {
	$("#publishProduct").empty();
	if (date == 0) {
		var dom3 = "<option value='00'" + (productId == "00" ? " selected='selected' " : "") + ">全选</option>";
		var dom2 = "<option value='PAYMENT'" + (productId == "PAYMENT" ? " selected='selected' " : "") + ">业主贝贝 </option>";
		var dom1 = "<option value='CHARGE'" + (productId == "CHARGE" ? " selected='selected' " : "") + ">物业贝贝</option>";
		$("#publishProduct").append(dom3);
		$("#publishProduct").append(dom2);
		$("#publishProduct").append(dom1);
	}
	if (date == 1) {
		var dom = "<option value='PAYMENT'" + (productId == "PAYMENT" ? " selected='selected' " : "") + ">业主贝贝 </option>";
		$("#publishProduct").append(dom);
	}
	if (date == 2) {
		var dom = "<option value='CHARGE'" + (productId == "CHARGE" ? " selected='selected' " : "") + ">物业贝贝</option>";
		$("#publishProduct").append(dom);
	}
}

function initCommmunity() {
	$("#cid").children().remove();
	var url = '/CMS/select/getCommunityList.ht';
	var pcid = $("#pcid").val()
	if (pcid) {
		url += '?pcid=' + pcid;
	}
	initCommunitySelect2("cid", url, null, false, "cName");
}

function initUl(data) {
	$("#communityUl").children().remove();
	if (data.length > 0) {
		if (data.length > 3)
			$("#communityUl").append("<li class=\"checkboxAll\" style=\"text-align: left;text-indent: 20px;\"><div class=\"control-checkbox\"><input type=\"checkbox\" /></div>（全选）</li>");
		$.each(data, function(index) {
			var d = "<li class=\"box-checkbox\"><div class=\"control-checkbox\" data-control-type=\"checkbox\"><input type=\"checkbox\" name=\"selectCid\" value='" + this.cid + "'/></div>" + this.communityName + "</li>";
			$("#communityUl").append(d);
		});
	} else {
		$("#communityUl").append("<li>没有直属此公司的小区</li>");
	}
	;
	var controlCheckbox = $("#communityUl").find(".box-checkbox .control-checkbox");
	var checkboxAll = $("#communityUl").find(".checkboxAll .control-checkbox");
	controlCheckbox && controlCheckbox.each(function() {
		YW.newObj(YW.controls.Checkbox, this, $(this).attr("data-control-config"));
	});
	checkboxAll && checkboxAll.each(function() {
		YW.newObj(YW.controls.CheckboxAll, this, $(this).attr("data-control-config"));
	});
}

function initUlForUpdate() {
	$("#communityUl").children().remove();
	if (data) {
		if (data.length > 0) {
			$.each(data, function(index) {
				var d = "<li class=\"box-checkbox\">" + this.cName + "</li>";
				$("#communityUl").append(d);
			});
		} else {
			$("#communityUl").append("<li>没有直属此公司的小区</li>");
		}
		;
	} else {
		$("#communityUl").append("<li>没有直属此公司的小区</li>");
	}
}

function search() {
	if ($("#infoType").val() == 2) {
		var companyId = $('#pcid').val();
		var _url = '/CMS/information/initCommunity.ht';
		if (!companyId)
			return;
		$.ajax({
			url : _url,
			data : {
				"companyId" : companyId,
			},
			type : 'post',
			beforeSend : showProccessBar,
			complete : hideProccessBar,
			success : function(data) {
				initUl(data.result);
			}
		});
	}
}

function returnBack() {
	var url = '/CMS/information/showList.ht';
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function toSubmit() {
	if ($('#infoType').val() == 2) {
		if ($("input[name=eMessageObject]:checked").val() == 2) {
			if ($("input[name=selectCid]:checked").length <= 0) {
				_alert("未选择小区");
				return;
			}
		}
	}

	var url = '/CMS/information/showList.ht';
	var form = $('#frm');
	//$("#content").val(editor.$txt.html());
	$("#content").val(editor.txt.html());
	$.submit(form, {
		dataType : 'json',
		beforeSend : showProccessBar,
		error : hideProccessBar,
		success : function(data, textStatus) {
			if (data.code == 1) {
				_alert("发布成功");
			} else {
				_alert("发布失败");
			}

			$.go(url, {
				dataType : 'html',
				error : function() {
					hideProccessBar();
				},
				success : function(data, textStatus) {
					replaceDocument(url, data);
				}
			});
		}
	});
}

// 初始化七牛上传


function printLog(title, info) {
	//window.console && console.log(title, info);
}